using System;
using System.Collections.Generic;
using System.Text;

namespace PetShop.Domain.Model
{
    [Serializable]
	public class CartInfo
	{

        private List<CartItemInfo> _items; 

        public  CartInfo()
        {
            _items = new List<CartItemInfo>();
        }

        private decimal _total = 0;

        public List<CartItemInfo> Items
        {
            get
            {
                return _items;
            }
        }
        public decimal Total
        {
            get { return _total; }
            set { _total = value; }
        }

        public int Count
        {
            get { return Items.Count; }
        }

        public CartItemInfo this[int index]
        {
            get { return Items[index]; }
        }

        public bool Exists(string itemId)
        {

            bool found = false;
            foreach (CartItemInfo cartItem in _items)
            {
                if (itemId == cartItem.Item.Id)
                {
                 
                    found= true;
                    return found;
                }
            }
            return found;
        }

        public void Remove(string itemId)
        {

            foreach (CartItemInfo cartItem in _items)
            {
                if (itemId == cartItem.Item.Id)
                {
                    _items.Remove(cartItem);
                    decimal totalitem = cartItem.Price * cartItem.Quantity;
                    _total = _total - totalitem;
                    return;
                }
            }
        }

        public void Add(CartItemInfo item)
        {
            _items.Add(item);
            _total = _total + (item.Price);
        }

        public void RemoveAt(int index)
        {
            CartItemInfo item = _items[index];
            decimal totalitem = item.Price * item.Quantity;
            _total = _total - totalitem;
            _items.RemoveAt(index);
        }

   
	}
}
